package com.example.smartcity_poverty.ui.AllService.SeeMovie

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import android.view.inputmethod.EditorInfo
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.smartcity_poverty.App.Companion.url
import com.example.smartcity_poverty.R
import com.example.smartcity_poverty.logic.Repository.api
import com.example.smartcity_poverty.logic.Repository.coroutine
import com.example.smartcity_poverty.logic.ServiceBean.LivingPayment.SmHomeListAllModel
import com.example.smartcity_poverty.logic.ServiceBean.SeeMovieBean.SmHomeBannerModel
import com.example.smartcity_poverty.logic.util.Util.glide
import com.example.smartcity_poverty.logic.util.Util.show
import com.example.smartcity_poverty.ui.AllService.SeeMovie.activity.SmShowActivity
import com.example.smartcity_poverty.ui.AllService.SeeMovie.adapter.SmHomeListAdapter
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import kotlinx.android.synthetic.main.activity_see_movie.*
import kotlinx.coroutines.Dispatchers


/**
 * 看电影功能实现
 */
class SeeMovieActivity : AppCompatActivity() {

    // 轮播图存储
    private val homeBannerList = ArrayList<SmHomeBannerModel.RowsBean>()

    // 电影列表存储
    private val homeList = ArrayList<SmHomeListAllModel.RowsBean>()

    // 默认刷新数据
    private var homeListSize = 6


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        setContentView(R.layout.activity_see_movie)
        title = "看电影"


        // 启动协程
        runThread()

        // 搜索查找电影
        getSeekEditText()

        // 加载更多
        getAdd()
    }


    private fun runThread() = coroutine() {
        // Banner
        getHomeBanner()
        // 电影列表
        getHomeList()
    }


    /**
     * 电影轮播图
     */
    private suspend fun getHomeBanner() {
        homeBannerList.clear()
        api.getSmHomeBanner().apply {
            if (this.code == "200") {
                runOnUiThread {
                    homeBannerList.addAll(this.rows!!)
                    // Banner适配器
                    smHomeBanner.apply {
                        this.adapter = object :
                            BannerImageAdapter<SmHomeBannerModel.RowsBean>(homeBannerList) {
                            override fun onBindView(
                                p0: BannerImageHolder?,
                                p1: SmHomeBannerModel.RowsBean?,
                                p2: Int,
                                p3: Int
                            ) {
                                glide(url + p1?.advImg, p0?.imageView!!)
                            }
                        }
                        this.addBannerLifecycleObserver(this@SeeMovieActivity).indicator =
                            CircleIndicator(context)
                        this.setBannerRound(25f)
                        this.setOnBannerListener { _, i ->
                            if (i == 0) {
                                startActivity(
                                    Intent(
                                        this@SeeMovieActivity,
                                        SmShowActivity::class.java
                                    ).putExtra("id", "15")
                                )
                            }

                            if (i == 1) {
                                startActivity(
                                    Intent(
                                        this@SeeMovieActivity,
                                        SmShowActivity::class.java
                                    ).putExtra("id", "16")
                                )
                            }

                            if (i == 2) {
                                startActivity(
                                    Intent(
                                        this@SeeMovieActivity,
                                        SmShowActivity::class.java
                                    ).putExtra("id", "17")
                                )
                            }

                        }
                    }
                }
            } else runOnUiThread { this.msg?.show() }
        }
    }


    /**
     * 电影列表适配器
     */
    private suspend fun getHomeList() {
        // 清空集合
        homeList.clear()
        api.getSmHomeListAll().apply {
            if (this.code == 200) {
                // 添加数据到集合
                homeList.addAll(this.rows!!)
                runOnUiThread {
                    smHomeListRv.apply {
                        this.isNestedScrollingEnabled = false
                        this.layoutManager = GridLayoutManager(context, 3)
                        this.adapter =
                            SmHomeListAdapter(homeListSize, homeList, R.layout.re_sm_home_list)
                        // 刷新数据
                        this.adapter?.notifyItemChanged(homeList.size)
                    }
                }
            } else runOnUiThread { this.msg?.show() }
        }
    }

    // 加载更多
    private fun getAdd() {
        smHomeAdd.setOnClickListener {
            if (homeListSize == 12) {
                runOnUiThread { smHomeAdd.text = "没有更多了..." }
            } else {
                coroutine(Dispatchers.Main) {
                    // 清空集合
                    homeList.clear()
                    api.getSmHomeListAll().apply {
                        if (this.code == 200) {
                            // 添加数据到集合
                            homeList.addAll(this.rows!!)
                            runOnUiThread {
                                smHomeListRv.apply {
                                    this.isNestedScrollingEnabled = false
                                    this.layoutManager = GridLayoutManager(context, 3)
                                    this.adapter = SmHomeListAdapter(
                                        homeListSize,
                                        homeList,
                                        R.layout.re_sm_home_list
                                    )
                                    // 刷新数据
                                    this.adapter?.notifyItemChanged(homeList.size)
                                }
                            }
                        } else runOnUiThread { this.msg?.show() }
                    }
                }
            }
            homeListSize += 6
        }

    }


    /**
     * 搜索
     */
    private fun getSeekEditText() {
        smHomeSeek.setOnEditorActionListener { _, i, _ ->
            if (i == EditorInfo.IME_ACTION_SEARCH) {
                if (smHomeSeek.text.toString().isNotEmpty()) {
                    // 清除数据
                    homeList.clear()
                    // 刷新界面UI
                    runOnUiThread { smHomeListRv.adapter?.notifyItemChanged(homeList.size) }
                    coroutine(Dispatchers.Main) {
                        // 发起网络请求
                        api.getSmSeekHomeListAll(smHomeSeek.text.toString()).apply {
                            if (this.code == 200) {
                                // 添加数据到集合
                                homeList.addAll(this.rows!!)
                                // upData UI
                                runOnUiThread {
                                    smHomeListRv.apply {
                                        this.isNestedScrollingEnabled = false
                                        this.layoutManager = GridLayoutManager(context, 3)
                                        this.adapter = SmHomeListAdapter(
                                            homeList.size,
                                            homeList,
                                            R.layout.re_sm_home_list
                                        )
                                        // 刷新数据
                                        this.adapter?.notifyItemChanged(homeList.size)
                                    }
                                }

                            } else runOnUiThread { this.msg?.show() }
                        }
                    }
                } else runOnUiThread { "不能为空！".show() }
            }
            false
        }
    }


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        finish()
        return super.onOptionsItemSelected(item)
    }
}